<template>
  <div class="user-list-box">
    <header>
      <el-input
        size="small"
        v-model="userListObj.name"
        placeholder="请输入内容"
      ></el-input>
      <el-button size="small" type="primary" @click="search">搜索</el-button>
    </header>
    <main>
      <el-table :data="tableData" height="250" border style="width: 100%">
        <el-table-column prop="id" label="id" width="200"> </el-table-column>
        <el-table-column prop="name" label="姓名" width="180">
        </el-table-column>
        <el-table-column prop="brand" label="品牌" />
        <el-table-column prop="price" label="价格" />
        <el-table-column prop="place" label="产地" width="200"/>
        <el-table-column prop="type" label="类型" />
        <el-table-column prop="unit" label="单位" />
        <el-table-column prop="level" label="品级" />
        <el-table-column prop="state" label="状态">
          <template slot-scope="scope">
            <div v-if="scope.row.state === 'new'" style="color:green">{{scope.row.state === 'new'? '新增' : scope.row.state === 'sales'? '销售中' : '下架'}}</div>
            <div v-else-if="scope.row.state === 'sales'" style="color:blue">{{scope.row.state === 'new'? '新增' : scope.row.state === 'sales'? '销售中' : '下架'}}</div>
            <div v-else style="color:red">{{scope.row.state === 'new'? '新增' : scope.row.state === 'sales'? '销售中' : '下架'}}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-sizes="[20, 40, 80]"
        :page-size="20"
        @size-change="pageSize"
        @current-change="page"
        @prev-click="topPage"
        @next-click="topPage"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </main>
  </div>
</template>
<script>
import { productListlApi } from "../../../apis/product";
export default {
  methods: {
    handleEdit(data){
     this.$router.push({path: '/product/edit', query: {data}})
    },
    productList() {
      productListlApi(this.userListObj).then((d) => {
        this.tableData = d.data.rows;
        this.total = d.data.count;
        console.log(d);
      });
    },
    search() {
      this.productList()
    },
    pageSize(size) {
      this.userListObj.size = size;
      this.productList()
    },
    page(page) {
      this.userListObj.page = page;
     this.productList()
    },
    topPage(page) {
      this.userListObj.page = page;
      this.productList()
    },
  },
  created() {
      this.productList()
  },
  data() {
    return {
      userListObj: {
        page: 1,
        size: 20,
        name: "",
        state:'',
      },
      total: 0,
      tableData: [],
    };
  },
};
</script>
<style lang="less" scoped>
.user-list-box {
  width: 100%;
  height: calc(100% - 48px);
  header {
    width: 100%;
    height: 48px;
    padding: 8px 10px;
    border-bottom: 1px solid #ccc;
    display: flex;
    justify-content: right;
    background: #fff;
    .el-input {
      width: 300px;
      margin: 0 5px;
      text-align: right;
    }
    .el-button {
      margin: 0 5px;
    }
  }
  main {
    height: calc(100% - 48px - 10px);
    .el-table {
      width: 100%;
      height: calc(100% - 48px) !important;
      padding: 10px;
    }
    .el-pagination {
      height: 48px;
      padding: 8px 0;
      text-align: center;
    }
  }
}
</style>